package com.zhkj.txg.module.login.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SPUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.login.vm.BindMobileViewModel
import kotlinx.android.synthetic.main.activity_login.*

class BindMobileActivity : ViewModelActivity<BindMobileViewModel>() {
    companion object {
        private val TYPE = "type"
        private val TOKEN = "token"
        private val USER_ID = "userId"
        val NOVICE_WELFARE = "noviceWelfare"
        fun loginBind(
            activity: Activity, token: String, userId: Long, requestCode: Int
        ) {
            val intent = Intent(activity, BindMobileActivity::class.java).putExtra(TYPE, 0)
                .putExtra(TOKEN, token).putExtra(USER_ID, userId)
            activity.startActivityForResult(intent, requestCode)
        }

        fun changeMobile(activity: Activity, requestCode: Int) {
            val intent = Intent(activity, BindMobileActivity::class.java).putExtra(TYPE, 1)
            activity.startActivityForResult(intent, requestCode)
        }
    }

    private val mobileFragment: BindMobileFragment by lazy { BindMobileFragment() }
    private val codeFragment: BindMobileCodeFragment by lazy { BindMobileCodeFragment() }

    override fun setViewModel(): BindMobileViewModel = createViewModel()
    override fun statusTitleBar(): View? = loginTitle
    override val layoutResId: Int = R.layout.activity_login

    override fun initUI() {
        // 判断是绑定手机号还是更换手机号
        loginTitle.showLeft(true).setLeftIcon(R.drawable.ic_arrow_left_black)
            .setTitleTextContent(getString(if (viewModel.type == 0) R.string.bind_mobile else R.string.change_mobile))

        showFragment(mobileFragment)
    }

    override fun initViewModel() {
        // 数据填充
        viewModel.type = intent.getIntExtra(TYPE, 0)
        if (viewModel.type == 0) {
            viewModel.token = intent.getStringExtra(TOKEN)
            viewModel.userId = intent.getLongExtra(USER_ID, 0)
        }
        // 页面跳转数据观察者
        viewModel.pageLiveData.observe(this, Observer {
            if (it) showFragment(codeFragment, true)
        })
        // 绑定手机数据观察者
        viewModel.bindMobileLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                toast(it.response?.msg ?: "绑定成功")
                SPUtil.getInstance().putString(NOVICE_WELFARE, it.response?.data?.noviceWelfare!!)
                setResult(RESULT_OK)
                finish()
            }
        })
        // 切换手机号码数据观察者
        viewModel.changeMobileLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                toast(it.response?.msg ?: "更换成功")
                setResult(RESULT_OK)
                finish()
            }
        })
    }

    private fun showFragment(fragment: Fragment, addToBackStack: Boolean = false) {
        supportFragmentManager.beginTransaction().apply {
            supportFragmentManager.fragments.forEach { hide(it) }
            if (!fragment.isAdded) add(R.id.loginContainer, fragment)
                .setMaxLifecycle(fragment, Lifecycle.State.RESUMED)
            if (addToBackStack) addToBackStack(null)
        }.show(fragment).commit()
    }
}